// pages/profile/friendInfo/index.ts
import tran from '../../../../utils/language'
import { commonTagList, commonRequestList } from '../../../../utils/api.js'
Component({
  pageLifetimes: {
    show() {
      this.getLangList()
      this.getProfession()
      this.getHelpList()
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    defaultParams: {
      type: Object,
      value: {},
      observer(newVal) {
        if (newVal && Object.keys(newVal).length > 0) {
          this.handleData(newVal)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _t: tran._t('masterStudy'),
    _tBase: tran._t('base'),
    type: 2,
    pickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: ''
    },
    ageList: [
      {
        selectFlag: false,
        text: '18及以下',
        value: '0-18'
      },
      {
        selectFlag: false,
        text: '18-25',
        value: '18-25'
      },
      {
        selectFlag: false,
        text: '26-32',
        value: '26-32'
      },
      {
        selectFlag: false,
        text: '33-41',
        value: '33-41'
      },
      {
        selectFlag: false,
        text: '42-55',
        value: '42-55'
      },
      {
        selectFlag: false,
        text: '55及以上',
        value: '55-200'
      }
    ],
    region: {},
    formData: {
      interestIds: '',
      professionId: '',
      personalities: '',
      professionName: '',
      languages: '',
      genderText: '',
      ageText: '',
      minAge: 0,
      maxAge: 0,
      online: 'all',
      currentProvinceCode: '',
      currentCityCode: '',
      currentDistrictCode: '',
      requestPlayText: '',
      requestPlayIds: '',
      friend: false,
      languageText: '',
      helpText: '',
      requestHelpIds: '',
    },
    characterTypeList: ['ENTP', 'INTJ', 'ENTJ', 'ENFP', 'INFP'],
    ageRangeList: [
      ['15', '20', '25', '30'],
      ['25', '30', '35', '40'],
    ],
    multiIndex: [0, 0],
    isEdit: getApp().globalData.profileFrom === 'edit',
    filterData: [{ text: '全部', key: 'all', selectFlag: true }, { text: '仅在线', key: 'online', selectFlag: false }],
    relationshipFilterData: [{ text: '全部', key: 'all', selectFlag: true }, { text: '仅在线', key: '321431', selectFlag: false }, { text: '新添加', key: '321000', selectFlag: false }],
    additionalFlag: false,
    knowledgeSelectList: [
      {
        text: '金融',
        key: '123'
      },
      {
        text: '会计',
        key: '456'
      },
      {
        text: '税务',
        key: '789'
      }
    ],
    commonPickerAgeParams: {
      show: false,
      pickerType: 'age',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    pickerParams2: {
      show: false,
      pickerType: 'skill',
      defaultValue: '',
      title: '职业'
    },
    commonPickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    helpParams: {
      show: false,
      pickerType: 'help',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    // helpList: ['帮助我提交纳税申报表', '帮助我写申请表'],
    helpList: [],
    commonPickerData: [] as commonPickerDataType[],
    langList: [],
    commonPickerType: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 处理父组件带过来的参数，给表单回显
     */
    handleData(obj: any) {
      console.log(obj, '晒');
      let { commonPickerParams, commonPickerAgeParams, formData, pickerParams2, filterData, relationshipFilterData, pickerParams, helpParams } = this.data
      commonPickerParams.defaultValue = obj.languages || ''
      formData.languageText = obj.languageText || ''
      commonPickerAgeParams.defaultValue = obj.ageText || ''
      pickerParams2.defaultValue = obj.professionId || ''
      formData.professionName = obj.professionName || ''
      formData.personalities = obj.personalities || ''
      formData.genderText = obj.genderText || ''
      formData.helpText = obj.helpText || ''
      formData.requestHelpIds = obj.requestHelpIds || ''
      helpParams.defaultValue = obj.requestHelpIds || ''
      formData.ageText = obj.ageText || ''
      pickerParams.defaultValue = obj.personalities || ''
      if (obj.online) {
        filterData.map(item => {
          if (item.key == obj.online) {
            item.selectFlag = true
          } else {
            item.selectFlag = false
          }
        })
      }
      this.setData({
        commonPickerParams,
        commonPickerAgeParams,
        formData,
        pickerParams,
        filterData,
      })
    },
    resetTap() {
      let formData = {
        interestIds: '',
        characterType: '',
        professionId: '',
        personalities: '',
        professionName: '',
        interestName: '',
        languageText: '',
        languages: '',
        genderText: '',
        gender: '',
        ageText: '',
        minAge: 0,
        maxAge: 0,
        online: 'all',
        currentProvinceCode: '',
        currentCityCode: '',
        currentDistrictCode: ''
      }
      this.setData({
        formData
      })
    },
    confirmPopup() {
      this.triggerEvent('confirmPopup', this.data.formData)
    },
    onClose() {
      this.triggerEvent('onClose')
    },

    additionalTap() {
      this.setData({
        additionalFlag: !this.data.additionalFlag
      })
    },
    toFriendSelectPage(e: any) {
      const currentIndex = e.currentTarget.dataset.index
      const { relationshipFilterData, formData } = this.data
      relationshipFilterData.length > 0 && relationshipFilterData.map((item, index) => {
        if (index === currentIndex) {
          item.selectFlag = true
          if (currentIndex == 1) {
            formData.friend = true
          } else if (currentIndex == 2) {
            formData.friend = false
          } else {
            formData.friend = null
          }
        } else {
          item.selectFlag = false
        }

      })
      this.setData({
        relationshipFilterData,
        formData
      })
    },

    /**
     * 清空选项列表
     */
    clearSelectList() {
      this.setData({
        selectList: []
      })
    },

    handleButtonTap(event: any) {
      // console.log(22, event);
      //阻止默认滑动事件
      // event.preventDefault();
      // event.stopPropagation()
    },
    toSelectPage(e: any) {
      const currentIndex = e.currentTarget.dataset.index
      const { filterData } = this.data
      filterData.length > 0 && filterData.map((item, index) => {
        if (index === currentIndex) {
          item.selectFlag = true
        } else {
          item.selectFlag = false
        }
      })
      this.setData({
        filterData
      })
    },
    searchFn() {
      const { formData } = this.data
      if (!formData.requestHelpIds) {
        wx.showToast({
          title: '请选择需要什么帮助',
          icon: 'none'
        })
        return false
      }
      const infoStr = JSON.stringify(formData)
      wx.navigateTo({
        url: `/pages/profile/friendResult/index?info=${infoStr}&type=2`
      })
    },
    // 删除某一种选中的选项
    delTap(e: any) {
      const { knowledgeSelectList } = this.data
      const { index } = e.currentTarget.dataset
      // 删除选中的下标的一项元素
      knowledgeSelectList.splice(index, 1)
      this.setData({
        knowledgeSelectList
      })
    },

    /**
     * 获取寻求帮助列表
     */
    getHelpList() {
      const params = {
        requestType: 'HELP'
      }
      commonRequestList(params).then(res => {
        res.length > 0 && res.map((item: any) => {
          item.text = item.reqName
          item.value = item.reqTypeId
        })
        this.setData({
          helpList: res
        })
      })
    },
    handlePicker(e: baseEvent) {
      // 打开picker 设置对应的picker弹出类型
      const type = e.currentTarget.dataset.type;
    },
    pickerCancel(e: { detail: any }) {
      if (e.detail) {
        // 有值表示是确认
        console.log(e.detail);
        this.setData({ formData: { ...this.data.formData, ...e.detail } })
      }
      this.setData({ pickerParams: { show: false, pickerType: '', defaultValue: '' } })
    },
    bindMultiPickerColumnChange(e: any,) {
      let columnIndex = e.detail.column
      let valueIndex = e.detail.value
      let { multiIndex } = this.data
      if (columnIndex == 0) {
        multiIndex[0] = valueIndex
      } else {
        multiIndex[1] = valueIndex
      }
      this.setData({
        multiIndex
      })
    },
    helpChange(e: any) {
      const { formData, helpList } = this.data
      const index = e.detail.value
      formData.help = helpList[index]
      this.setData({
        formData
      })
    },
    characterTypeChange(e: any) {
      const { formData, characterTypeList } = this.data
      const index = e.detail.value
      this.setData({
        formData
      })
    },
    bindRegionChange(e: any) {
      this.setData({
        region: e.detail.value
      })
    },
    /**
     * 从app.ts里面获取全局的语言列表
     */
    getLangList() {
      let list = getApp().globalData.langList
      let langList: any = [
        {
          selectFlag: false,
          text: '全部',
          value: 'all'
        }
      ]
      list.length > 0 && list.map((item: any) => {
        const obj = {
          selectFlag: false,
          ...item
        }
        langList.push(obj)
      })
      this.setData({
        langList
      })
    },
    handleHelpPicker(e: baseEvent) {
      const { helpParams, langList } = this.data
      const type = e.currentTarget.dataset.type;
      helpParams.pickerType = type
      helpParams.show = true
      this.setData({ helpParams, commonPickerData: langList, commonPickerType: type })
    },
    handleCommonPicker(e: baseEvent) {
      const { commonPickerParams, langList } = this.data
      const type = e.currentTarget.dataset.type;
      commonPickerParams.pickerType = type
      commonPickerParams.show = true
      this.setData({ commonPickerParams, commonPickerData: langList, commonPickerType: type })
    },
    handleCommonSkillPicker(e: baseEvent) {
      const { pickerParams2 } = this.data
      const type = e.currentTarget.dataset.type;
      pickerParams2.pickerType = type
      pickerParams2.show = true
      this.setData({ pickerParams2, commonPickerType: type })
    },
    handleCommonAgePicker(e: baseEvent) {
      const { commonPickerAgeParams } = this.data
      // 打开picker 设置对应的picker弹出类型
      const type = e.currentTarget.dataset.type;
      commonPickerAgeParams.pickerType = type
      commonPickerAgeParams.show = true
      this.setData({ commonPickerAgeParams, commonPickerType: type })
    },

    /**
     * 普通的选择器确认按钮
     * @param e 
     */
    commonPickerConfirm(e: any) {
      const { commonPickerType, helpParams, commonPickerParams, pickerParams2, commonPickerAgeParams } = this.data
      const obj = e.detail.value
      const { formData } = this.data
      if (commonPickerType == 'language') { // 语言
        formData.languageText = obj.text
        formData.languages = obj.value
        commonPickerParams.defaultValue = obj.value
      } else if (commonPickerType == 'help') { // 帮助
        formData.helpText = obj.text
        formData.requestHelpIds = obj.value
        helpParams.defaultValue = obj.value
      } else if (commonPickerType == 'age') { // 年龄范围
        formData.ageText = obj.text
        const list = obj.value.split('-')
        formData.minAge = list[0]
        formData.maxAge = list[1]
        commonPickerAgeParams.defaultValue = obj.value
      } else if (commonPickerType == 'skill') { // 技能
        formData.professionName = obj.text
        formData.professionId = obj.value
        pickerParams2.defaultValue = obj.value
      }
      helpParams.show = false
      commonPickerParams.show = false
      pickerParams2.show = false
      commonPickerAgeParams.show = false
      this.setData({
        formData,
        helpParams,
        commonPickerParams,
        pickerParams2,
        commonPickerAgeParams,
      })
    },
    /**
      * 获取职业技能列表
      */
    getProfession() {
      const listAll = wx.getStorageSync('professionList')
      let list = [{
        text: '全部',
        value: null,
        selectFlag: false,
        tagTypeId: ''
      }]
      listAll.length > 0 && listAll.map((item: any) => {
        item.text = item.tagName
        item.selectFlag = false
        item.value = item.tagTypeId
        list.push(item)
      })
      this.setData({
        selectList: list,
        professionList: list
      })
    },

  }
})